package com.itheima.controller;

import com.itheima.domain.Fruit;
import com.itheima.domain.User;
import com.itheima.service.ManagerService;
import com.itheima.service.UserService;

import java.util.ArrayList;
import java.util.Scanner;

public class UserController {
    UserService userService = new UserService();
    Scanner sc = new Scanner(System.in);
    private ManagerService managerService = new ManagerService();
    Double  allPrice ;
    String id = null;
    String fruitname;
    int number;
    public final void start() {
        //登录功能
        userAdmain();
        userloop:
        while (true) {



            System.out.println("请输入您的选择: 1.查看水果  2.开始购买  3.结账  4积分兑换  5.退出");
            String choice = sc.next();
            switch (choice) {

                case "1":
                    //查看水果
                    selectFruit();
                    break;
                case "2":
                    //开始购买
                    buyFruit();
                    break;
                case "3":
                    //结账
                    checkOut();
                    break;
                case "4":
                    pointsExchange();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break userloop;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }


    //积分兑换
    private void pointsExchange() {
        User u = managerService.selectById(id);

        System.out.println("您当前的积分为：" + u.getPoints());
        System.out.println("请选择商品兑换：1.西瓜（积分200）  2.哈密瓜（积分200）  3.火龙果（积分120）  4.退出");
        String choice = sc.next();
        switch (choice) {
            case "1":
                managerService.pointsExchange(id, 200);
                break;
            case "2":
                managerService.pointsExchange(id, 200);
                break;
            case "3":
                managerService.pointsExchange(id, 120);
                break;
            case "4":
                return;
            default:
                System.out.println("输入有误，请重新输入");
        }
    }

    private void checkOut() {
        System.out.println("原价:"+allPrice);
        double finalAllPrice=userService.free(allPrice);
        System.out.println("优惠后价格："+finalAllPrice);
        System.out.println("输入 y  结账 ，输入 n 退出");
        String cont = sc.next();
        if (cont.equals("y")){
            userService.checkOut(finalAllPrice,id,fruitname,number);
            System.out.println("收银结束");
        }
        if (cont.equals("n")){
            System.out.println("请确认身份，重新登录账号");
            System.out.println("-------------------------------------");
            start();
        }
    }

    private void buyFruit() {
        while (true) {
            selectFruit();
            System.out.println("-------------------------------------");
            System.out.println("请输入您要购买的水果  输入n退出");


            fruitname = sc.next();
            if (fruitname.equals("n")) {
                break;
            } else
                System.out.println("请输入购买的数量");
            number = sc.nextInt();
            if(userService.haveFruit(fruitname,number)){
                System.out.println("库存不足");
                break;
            }

           allPrice= userService.buyFruit(fruitname, number);

           if(allPrice==0){
               System.out.println("无此水果或已售罄");
allPrice++;
               break;
           }
            System.out.println("目前总价为"+allPrice);
            System.out.println("重新购买请输入 n ,输入任意键保存 ");
            String cont = sc.next();
            if(!cont.equals("n")){
                break;
            }

        }
    }


    private void selectFruit() {
        ArrayList<Fruit> list = userService.selectFruit();
        System.out.println("水果编号  水果名称  水果价格  水果数量");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "     " + list.get(i).getName() + "     " + list.get(i).getPrice() + "     " + list.get(i).getAmount());
        }
    }

    //获取登录账号中的用户账号用作积分兑换参数
    private String userAdmain() {

        while (true) {
            System.out.println("--------亲爱的顾客，请登录您的账号--------");
            System.out.println("-------------------------------------");
            System.out.println("请输入您的账号");
            id = sc.next();
            System.out.println("请输入您的密码");
            String password = sc.next();
            if (userService.userAdmain(id, password)) {
                System.out.println("登录成功，欢迎您");
                System.out.println("-------------------------------------");
                break;
            } else
                System.out.println("账号或密码错误，请检查后重试");
            System.out.println("-------------------------------------");


        }


        return id;
    }
}
